import base from '../../scripts/base'
import utils from '../../scripts/utils'
import './inviteprize.css'
import $ from 'jquery'

utils.baiduStatistics()


$(function () {
    $('#download').on('click', function () {
        window.location.href = 'download1.html';
    })
    $('#receive').on('click', function () {
        receivegift();
    })

    //判断是否是微信
    if (isWeChat()) {
        $('body').css('background', '#002826');
        $(".div1").show();
        $(".wrap").hide();
    } else {
        $(".div1").hide();
        $(".wrap").show();
    }
})


var g_baseScheme = "wxccf8c35e6491babc://"
var pid = getQueryString('pid');
//iframe生成器
var createIframe = (function () {
    var iframe;
    return function () {
        if (iframe) {
            return iframe;
        } else {
            iframe = document.createElement('iframe');
            iframe.style.display = 'none';
            document.body.appendChild(iframe);
            return iframe;
        }
    }
})()

var isIos = function () {
    var ua = navigator.userAgent.toLowerCase();
    if (/(iPhone|iPad|iPod|iOS)/i.test(ua)) {
        return true;
    }
    return false;
}

var isAndroid = function () {
    var ua = navigator.userAgent.toLowerCase();
    if (/Android/i.test(ua)) {
        return true;
    }
    return false;
}

var isWeChat = function () {
    var ua = navigator.userAgent.toLowerCase();
    if (/MicroMessenger/i.test(ua)) {
        return true;
    } else {
        return false;
    }
}

/*
 打开app
 @param command 和app约定好的命令
 */
var openApp = function (command) {
    //启动并进入房间
    var openIframe = createIframe();
    if (isIos()) {
        var ver = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
        ver = parseInt(ver[1], 10);
        $('#scrolltext').attr('scrollamount', '3');
        if (ver < 9) {
            window.location = command;
            $('#grayback').css('display', 'flex');
            setTimeout(function () {
                hideLoadding();
            }, 2000);
        } else {
            window.location = command;
            $('#grayback').css('display', 'flex');
            setTimeout(function () {
                hideLoadding();
            }, 2000);
        }
    } else {
        //其它平台
        openIframe.src = command;
        $('#grayback').css('display', 'flex');
        setTimeout(function () {
            hideLoadding();
        }, 2000);
    }
}

function hideLoadding() {
    $('#grayback').hide();
    $('.uninstall').css('display', 'flex');
    document.getElementById('scrolltext').start();
}

var receivegift = function () {
    //获取当前URL
    var thisURL = document.URL;
    var startkey = thisURL.indexOf('?');
    //获取参数
    var param = thisURL.substr(startkey + 1, thisURL.length - 1);
    var isappinstalled = true;
    var command = g_baseScheme + "action=invite&playerid=" + pid;

    var downloadUrl = "http://hnyy.happypoker.cn/download.html"; //外网download页面

    if (isappinstalled) { //如果本机装有APP 执行打开APP流程
        // include_js("./openapp/"+ getQueryString('action') +".js",0);
        if (isWeChat()) {
            $('body').css('background', 'rgb(0,57,98)');
            $(".div1").show();
            $(".wrap").hide();
        } else {
            if (param == null) {
                return;
            }
            openApp(command);
        }
    } else { //如果本机没有安装APP 直接跳转下载页面
        if (!isWeChat()) {
            window.location.href = downloadUrl;
        }
    }
}

function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}
document.write = function (s) {
    document.getElementById('jsdiv').innerHTML += s;
    return false;
}

function include_js(path, reload) {
    var scripts = document.getElementsByTagName("script");
    if (!reload)
        for (var i = 0; i < scripts.length; i++)
            if (scripts[i].src && scripts[i].src.toLowerCase() == path.toLowerCase()) return;
    var sobj = document.createElement('script');
    sobj.type = "text/javascript";
    sobj.src = path;
    var headobj = document.getElementsByTagName('head')[0];
    headobj.appendChild(sobj);
}